import { defineStore } from "pinia";
import { Upload } from "@/stores/interface";
// import piniaPersistConfig from "@/config/piniaPersist";
import { handleFile, sliceUpload } from "@/utils/file/index";
import { uploadFile } from "@/api/modules/file";
export const useUploadStore = defineStore({
  id: "hft_upload",
  state: (): Upload.UploadState => ({
    list: [],
    isShow: false
  }),
  getters: {},
  actions: {
    async handleFile(files: any, pid: number = 0) {
      for (let i = 0; i < files.length; i++) {
        if (files[i].need_slice) {
          const { totalChunks } = handleFile(files[i].file, files[i].chunk);
          this.list.push({ uuid: files[i].uuid, chunkTotal: totalChunks, nowIndex: 0, filename: files[i].filename, status: 0 });
        } else {
          this.list.push({ uuid: files[i].uuid, chunkTotal: 1, nowIndex: 0, filename: files[i].filename, status: 0 });
        }
      }
      for (let i = 0; i < files.length; i++) {
        if (files[i].need_slice) {
          const { chunks, totalChunks } = handleFile(files[i].file, files[i].chunk);
          await sliceUpload(
            files[i].filename,
            files[i].file.type,
            files[i].uuid,
            totalChunks,
            chunks,
            pid,
            files[i].size,
            uploadFile,
            this.list
          );
        } else {
          await sliceUpload(
            files[i].filename,
            files[i].file.type,
            files[i].uuid,
            1,
            [files[i].file],
            pid,
            files[i].size,
            uploadFile,
            this.list
          );
        }
      }
      files = null; //清理
    },
    showDialog(show: boolean) {
      this.isShow = show;
    }
  }
  // persist: piniaPersistConfig("hft_upload")
});
